package net.da.bof;

import java.util.Collection;

import net.da.DaStarter;
import net.da.core.bof.ApplicationSession;
import net.da.core.bof.OrganizationUnit;
import net.da.core.bof.OrganizationUser;
import net.da.core.bof.SysUser;
import net.da.core.exception.DaException;
import net.da.services.SecurityService;
import net.da.services.spi.local.SecurityServiceImpl;
import junit.framework.TestCase;

public class SecurityServiceTest extends TestCase {

    public void testOrganisationUserCreation() {
        SecurityService securityService = new SecurityServiceImpl();
        DaStarter.getInjector().injectMembers(securityService);

        SysUser sysUser = securityService.createNewAdminUser();
        sysUser.setName("admin");
        sysUser.setPassword("Password01;");
        sysUser.setDescription("Administrative user");
        try {
            sysUser.save(null);
        } catch (DaException e1) {
            assertTrue(false);
        }

        ApplicationSession session = null;
        try {
            session = securityService.authenticateSysUser("admin", "Password01;");
        } catch (DaException e) {
            assertTrue(false);
        }

        OrganizationUnit organisationUnit = securityService.createNewOrganisationUnit(session);
        organisationUnit.setName("Realty agency 1");
        organisationUnit.setAddress("g. Kiev, ul. Malinovskogo, d. 10");
        try {
            organisationUnit.save(session);
        } catch (DaException e) {
            assertTrue(false);
        }

        OrganizationUser organisationUser = securityService.createNewOrganisationUser(session, organisationUnit);
        organisationUser.setFirstName("Dmitry");
        organisationUser.setLastName("Dzifuta");
        organisationUser.setLogin("likon");
        organisationUser.setPassword("password");
        organisationUser.setOrganisationUnit(organisationUnit);
        try {
            organisationUser.save(session);
        } catch (DaException e) {
            assertTrue(false);
        }

        organisationUnit = securityService.getOrganisationUnit(organisationUnit.getId());

        Collection<OrganizationUser> orgUsers = organisationUnit.getOrganisationUsers();
        assertEquals(orgUsers.size(), 1);

    }
}
